<template>
  <div class="main">
    <el-cascader
      placeholder="试试搜索：指南"
      :options="options"
      filterable>
    </el-cascader>
  </div>
</template>

<script>
  import store from '@/store'
  import {add} from '@/api/thk/store'
  import { client } from '@/utils/alioss'
  import {dateFormat} from '@/utils/dateFormat'

  export default {
    name: 'addStore',

    data() {
      return {
        showAddStoreDialog: false,
        formLabelWidth: '130px',
        storeData: {
          customer_id: '',
          name: '',
          province: '',
          city: '',
          country: '',
          address: '',
          contact: '',
          telephone: '',
          wechat_qrcode: '',
          // date_added: '',
          // date_modified: '',
        },
        customerList: [],
        provinceAndCityList: [],
        organizationList: [],

        // 这两个字段为当customer对应的store已存在时，不能增加所显示的提示信息
        msg: "",
        isMsg : false,
        // 表单校验规则
        rules: {
          // 店铺名为必须
          name:[
            { required: true, message: '请输入店铺名称', trigger: 'blur' },
          ],
          telephone:[
            { required: true, message: '请输手机号', trigger: 'blur' },
            { min: 11, max: 11, message: '长度必须为11位', trigger: 'blur' }
          ],
          customer_id: [
            { required: true, message: '请选择用户', trigger: 'change' }
          ],

          contact: [
            { required: true, message: '请选择联系人', trigger: 'change' }
          ],
        }
      }
    },



    methods: {
      submitStore(storeData){
        // 设置添加的时间
        this.$refs[storeData].validate((valid) => {
          console.log('store data >>>>>>>')
          console.log(this.storeData)
          if (valid) {
            add(store.getters.token,this.storeData).then(result => {
              // 增加成功
              this.showAddStoreDialog = !this.showAddStoreDialog

              // 一个用户只能有一个店铺，判断创建用户的店铺是否存在(存在则alert一个错误)
              if(result.data.status === false){
                this.msg = result.data.msg
                this.isMsg = true
                setTimeout(()=>{
                  this.isMsg = false
                },3000)
                return
              }else{
                this.$message.success('增加成功!')
              }

              this.$parent.getStoreList()
            }).catch(error => {
              this.$message('增加失败!')
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });

      },

      cancelSubmit(customerList,provinceAndCityList,organizationList){
        this.customerList = customerList
        this.provinceAndCityList = provinceAndCityList
        this.organizationList = organizationList
        this.showAddStoreDialog = !this.showAddStoreDialog
        this.changeCityData()
      },

      // 转换省份数据
      changeCityData(){
        console.log("changeCityList",this.provinceAndCityList)
        let data = {data: this.provinceAndCityList}
        let str = JSON.stringify(data).replace(/province_id/g,'value')
        str = str.replace(/province/g,'label')

        str = str.replace(/city_id/g,'value')
        str = str.replace(/city/g,'label')

        str = str.replace(/country_id/g,'value')
        str = str.replace(/country/g,'label')

        data = JSON.parse(str)
        this.provinceAndCityList = data.data
      },

      handleChange(value){
        if(value){
          this.organizationData.province = value[0]
          this.organizationData.city = value[1]
          this.organizationData.country = value[2]
        }
      },

      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isPNG = file.type === 'image/png'
        const isLt2M = file.size / 1024 / 1024 < 1
        if (!isJPG && !isPNG) {
          this.$message.error('上传图片只能是 JPG、PNG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 1MB!')
        }
        return (isJPG | isPNG) && isLt2M
      },
      upload_picture(file) {
        const that = this
        const fileName = 'user-admin_user-picture' + file.file.uid
        client().put(fileName, file.file).then((res) => {
          this.$set(that.storeData, 'wechat_qrcode', res.url)

          // that.storeList.wechat_qrcode = res.url

          console.log(res.url);
          that.$message.success('头像地址上传成功！')
        }).catch((err) => {
          that.$message.error('头像地址上传失败，原因：' + err)
        })
      },

      // 关闭alert的错误信息
      closeMsg(){
        this.isMsg = false
      }


    }
  }
</script>

<style>
  /* 图片上传的样式 */
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    color: #8c939d;
    text-align: center;
  }
  .avatar {
    display: block;
  }

  /* 头像地址(head_img)图片上传组件的尺寸 */
  .upload_head_img .avatar-uploader .el-upload{
  }
  .upload_head_img .avatar-uploader-icon{
    font-size: 28px;
    width: 80px;
    height: 80px;
    line-height: 80px;
  }
  .upload_head_img .avatar{
    width: 80px;
    height: 80px;
  }

  .alert-msg{
    position: absolute;
    height: 30px;
    width: 300px;
    top: 10px;
    left: 500px;
  }

</style>



